package controllers

import (
	"encoding/json"
	"io"
	"xmgl/models"

	"strconv"

	"log"

	"github.com/astaxie/beego"
)

/*上课节次 类型的API*/
type Api_skjcController struct {
	beego.Controller
}

//取得上课节次列表或根据ID返回单个节次
func (this *Api_skjcController) Get() {
	var (
		//err error
		id    int
		skjc  models.Sut_sys
		skjcs []models.Sut_sys
	)
	id, _ = this.GetInt(":id", -1) //取数字值
	//log.Println("this.Ctx.Input.Param(':id'):", gzlx_id)
	if id == -1 {
		//提交的没有ID号这个字段，则返回所有
		for j := 0; j <= 16; j++ {
			skjc = models.Sut_sys{j, strconv.Itoa(j)}
			skjcs = append(skjcs, skjc)
		}
	} else {
		//取到了ID号，则只返回这个ID号的
		skjcs = append(skjcs, models.Sut_sys{id, strconv.Itoa(id)})
	}
	this.Data["json"] = skjcs
	this.ServeJSON()
}

/*故障类型的API*/
type Api_gzlxController struct {
	beego.Controller
}

//取得故障类型列表或根据ID返回单个故障类型
func (this *Api_gzlxController) Get() {
	var (
		//err error
		gzlx_id int
		gzlxs   []models.Sut_gzlx
	)
	gzlx_id, _ = this.GetInt(":id", -1) //取数字值
	//log.Println("this.Ctx.Input.Param(':id'):", gzlx_id)
	if gzlx_id == -1 {
		//提交的没有ID号这个字段，则返回所有工单
		gzlxs = models.Get_gzlx(0)
	} else {
		//取到了ID号，则只返回这个ID号的工单
		gzlxs = models.Get_gzlx(gzlx_id)
	}
	this.Data["json"] = gzlxs
	this.ServeJSON()
}

//API，返回设备的类型列表
type SblxsController struct {
	beego.Controller
}

//api,JSON，返回设备的所有类型
func (this *SblxsController) Get() {
	var (
		sblxs []models.Sut_sb
		json1 []byte
		err   error
	)
	url_type := this.GetString(":type")
	url_value, err := this.GetInt(":value")
	log.Println("SblxsController，查询并返回设备类型：?:type:", url_type, "，?:value:", url_value)
	checkErr(err)
	if url_type == "gzlx" {
		sblxs = models.Get_sblxs_gzlx(url_value) //根据工单类型（故障类型,url_value）查询对应的设备
	} else if url_type == "all" {
		sblxs = models.Get_sblxs(24) //24在数据库里是所有硬件设备的父ID号
	}
	if len(sblxs) > 0 {
		json1, err = json.MarshalIndent(sblxs, "", "")
		if err != nil {
			return
		}
		io.WriteString(this.Ctx.ResponseWriter, string(json1[:]))
	} else {
		io.WriteString(this.Ctx.ResponseWriter, "[]")
	}
}

/*故障处理方式的API*/
type Api_gzclfsController struct {
	beego.Controller
}

//返回故障处理方式列表
func (this *Api_gzclfsController) Get() {
	var (
		//err error
		type_id int
		sys     models.Sut_sys
		syss    []models.Sut_sys
	)
	type_id, _ = this.GetInt(":type_id", -1) //取数字值
	//log.Println("this.Ctx.Input.Param(':id'):", gzlx_id)
	if type_id == 0 {
		//提交的没有ID号这个字段，则返回所有
		syss = sys.Get_gzclfs(0)
	} else {
		//取到了ID号，则只返回这个ID号的
		syss = sys.Get_gzclfs(type_id)
	}
	this.Data["json"] = syss
	this.ServeJSON()
}

//返回URL生成的二维码的[]byte
type Api_ZbSjEwmController struct {
	beego.Controller
}

func (this *Api_ZbSjEwmController) Get() {
	//取得二维码：http://127.0.0.1/api/v1/qrcode?url=http://127.0.0.1/wxlive
	var (
		url    string
		qrcode models.Sut_qrcode
	)
	url = this.GetString("url", "")
	//log.Println("取二维码的网址URL:", url)
	//qrcode.Header = append(qrcode.Header, '') //ata:image/png;base64,
	//编码的png图片数据
	qrcode.Qrcode = qrcode.GetQrcode(url) //生成二维码字节数据
	//qrcode.Qrcode = append(qrcode.Header, qrcode.Qrcode...)
	//this.Data["json"] = qrcode.Qrcode
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png") //设置类型为png,base64
	//输出二维码的字节数据,html里显示用<img src="/api/v1/qrcode" alt="手机请扫码" />
	this.Ctx.ResponseWriter.Write(qrcode.Qrcode)
	//this.ServeJSON()	//serverjson不能使用this.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")设置content-type的类型
}
